home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Rotate Bitmap 90 / Rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.5 KB  |  193 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Rotate.c
  3.  
  4.     Contains:    Rotates a bitmap 90 degrees
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  20.                                             for demonstration purposes.
  21.                 7/14/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #include "CarbonPrefix.h"
  26. #include <Dialogs.h>
  27. #include <Fonts.h>
  28. #include <Processes.h>
  29. #include <Sound.h>
  30. #include <Gestalt.h>
  31.  
  32. #define    BIT00       0x01
  33. #define    BIT01       0x02
  34. #define    BIT02       0x04
  35. #define    BIT03       0x08
  36. #define    BIT04       0x10
  37. #define    BIT05       0x20
  38. #define    BIT06       0x40
  39. #define    BIT07       0x80
  40.  
  41. void rotate();
  42.  
  43. BitMap        *macBitMap;
  44.  
  45. void rotate()
  46. {
  47.     Ptr                srcPtr, destPtr, destColPtr, myBaseAddr;
  48.     BitMap            *newBitmap;
  49.     short            destRowBytes, bitchar, srcByte, srcH, srcV, srccols, srcrows, i;
  50.     Rect            destRect;
  51.  
  52.     /* rotate boundary rectangle */
  53.     srcH = macBitMap->bounds.right - macBitMap->bounds.left;
  54.     srcV = macBitMap->bounds.bottom - macBitMap->bounds.top;
  55.     SetRect(&destRect, macBitMap->bounds.left, macBitMap->bounds.top,
  56.             macBitMap->bounds.left+srcV, macBitMap->bounds.top+srcH);
  57.  
  58.     /* allocate destination buffer */
  59.     destRowBytes  = (srcV+7) >> 3;
  60.     newBitmap = (BitMap *) NewPtrClear(sizeof(BitMap));
  61.     myBaseAddr = (Ptr) NewPtrClear(destRowBytes*srcH);
  62.  
  63.     /* set-up src to rotated destination scan */
  64.     srccols = macBitMap->rowBytes;
  65.     srcrows = srcV;
  66.     srcPtr = macBitMap->baseAddr;
  67.     bitchar = BIT00;
  68.     destColPtr= myBaseAddr + destRowBytes - 1;
  69.     destPtr   = destColPtr;
  70.  
  71.     /* scan src row and rotate into dest col */
  72.     while (srcrows-- > 0) {
  73.         for (i=0; i<srccols; i++) {
  74.             srcByte = *(srcPtr++);
  75.             if (srcByte & BIT07) *destPtr |= bitchar;
  76.             destPtr += destRowBytes;
  77.             if (srcByte & BIT06) *destPtr |= bitchar;
  78.             destPtr += destRowBytes;
  79.             if (srcByte & BIT05) *destPtr |= bitchar;
  80.             destPtr += destRowBytes;
  81.             if (srcByte & BIT04) *destPtr |= bitchar;
  82.             destPtr += destRowBytes;
  83.             if (srcByte & BIT03) *destPtr |= bitchar;
  84.             destPtr += destRowBytes;
  85.             if (srcByte & BIT02) *destPtr |= bitchar;
  86.             destPtr += destRowBytes;
  87.             if (srcByte & BIT01) *destPtr |= bitchar;
  88.             destPtr += destRowBytes;
  89.             if (srcByte & BIT00) *destPtr |= bitchar;
  90.             destPtr += destRowBytes;
  91.         }
  92.         if (bitchar==BIT07) {
  93.             bitchar = BIT00;
  94.             destColPtr--;
  95.         } else {
  96.             bitchar <<= 1;
  97.         }
  98.  
  99.         destPtr = destColPtr;
  100.     }
  101.     
  102.     /* remove src bitmap (portbits for offscreen port) */
  103.     DisposePtr((Ptr)macBitMap->baseAddr);
  104.     DisposePtr((Ptr)macBitMap);
  105.     
  106.     /* store new src in object */
  107.     macBitMap   = (BitMap *)newBitmap;
  108.     macBitMap->baseAddr = (Ptr) myBaseAddr;
  109.     macBitMap->rowBytes = destRowBytes;
  110.     macBitMap->bounds   = destRect;
  111. }
  112.  
  113. void main()
  114. {
  115.     WindowPtr        mainWinPtr;
  116.     OSErr            error;
  117.     //SysEnvRec        theWorld;        //not available in carbon
  118.     long            result;            //used to check QD Version
  119.     Rect            windRect, ovalRect, myBounds;
  120.     short            myRowBytes;
  121.     RgnHandle        rgnHandle = NewRgn();
  122.     
  123.     /* Make sure ColorQD exists. */
  124.     /*error = SysEnvirons(1, &theWorld);
  125.     if (theWorld.hasColorQD == false) {
  126.         SysBeep(50);
  127.         ExitToShell();
  128.     }*/
  129.     
  130.     error = Gestalt(gestaltQuickdrawVersion, &result);
  131.     if (error != noErr || result < gestalt8BitQD) {
  132.         SysBeep(50);
  133.         ExitToShell();
  134.     }
  135.     
  136.     /* Initialize all the needed managers. */
  137.     /*InitGraf(&qd.thePort);
  138.     InitFonts();
  139.     InitWindows();
  140.     InitMenus();
  141.     TEInit();
  142.     InitDialogs(nil);*/
  143.     InitCursor();
  144.  
  145.     /* Define output window with an inset clip region. */
  146.     SetRect(&windRect, 100, 100, 404, 404);
  147.     mainWinPtr = NewWindow(nil, &windRect, "\pJohn", true, documentProc, (WindowPtr) -1, false, 0);
  148.     //SetPort(mainWinPtr);
  149.     SetPortWindowPort( mainWinPtr );
  150.  
  151.     SetRect(&windRect, 0, 0, 304, 304);
  152.     EraseRect(&windRect);
  153.     SetRect(&ovalRect, 15, 15, 60, 120);
  154.     PaintOval(&ovalRect);
  155.     MoveTo(10,140);
  156.     DrawString("\pHi there.");
  157.     MoveTo(0,0);
  158.     LineTo(303,303);
  159.     MoveTo(0,303);
  160.     LineTo(303,0);
  161.  
  162.     /* rotate boundary rectangle */
  163.     myBounds = windRect;
  164.     myRowBytes = (myBounds.right - myBounds.left + 7) >> 3;
  165.     macBitMap = (BitMap *) NewPtrClear(sizeof(BitMap));
  166.     macBitMap->baseAddr = (Ptr) NewPtrClear(myRowBytes * (myBounds.bottom - myBounds.top));
  167.     macBitMap->bounds = myBounds;
  168.     macBitMap->rowBytes = myRowBytes;
  169.  
  170.     //CopyBits(&(*mainWinPtr).portBits, macBitMap, &windRect, &macBitMap->bounds, 0, nil);
  171.     CopyBits(GetPortBitMapForCopyBits(GetWindowPort(mainWinPtr)), macBitMap, &windRect, &macBitMap->bounds, 0, nil);
  172.     QDFlushPortBuffer(GetWindowPort(mainWinPtr), GetPortVisibleRegion(GetWindowPort(mainWinPtr), rgnHandle));
  173.  
  174.     /* Wait until user clicks button. */
  175.     do {
  176.     } while (!Button());
  177.  
  178.     rotate();
  179.     //CopyBits(macBitMap, &(*mainWinPtr).portBits, &macBitMap->bounds, &windRect, 0, nil);
  180.     CopyBits(macBitMap, GetPortBitMapForCopyBits(GetWindowPort(mainWinPtr)), &windRect, &macBitMap->bounds, 0, nil);
  181.     QDFlushPortBuffer(GetWindowPort(mainWinPtr), GetPortVisibleRegion(GetWindowPort(mainWinPtr), rgnHandle));
  182.     
  183.     /* Wait for button to be up */
  184.     do {
  185.     } while (Button());
  186.     /* Wait until user clicks button. */
  187.     do {
  188.     } while (!Button());
  189.     
  190.     DisposeRgn(rgnHandle);
  191. }
  192.  
  193.